package cappuccino.couldbook.ui;

import android.database.Observable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import cappuccino.couldbook.R;
import cappuccino.couldbook.base.BaseActivity;
import cappuccino.couldbook.core.mananger.AccountM;
import cappuccino.couldbook.data.Group;
import cappuccino.couldbook.data.Result;
import cappuccino.couldbook.net.NetApi;
import cappuccino.couldbook.net.Network;
import cappuccino.couldbook.ui.Adapter.RecylerApplicantsAdapter;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/9/16.
 */
public class ApplicantActivity extends BaseActivity {
    RecyclerView rv;
    @Override
    protected void bindView() {
        setContentView(R.layout.activity_recylerview);
        rv = (RecyclerView) findViewById(R.id.rv_rv);
        final NetApi api = Network.getNetApi();
        AccountM accountM = new AccountM(this);
        int userId = accountM.getAccout();
        if(userId!=-1) {
            api.checkMananger(userId).map(new Func1<Result<List<Group>>, List<Group>>() {
                @Override
                public List<Group> call(Result<List<Group>> listResult) {
                    return listResult.data;
                }
            }).flatMap(new Func1<List<Group>, rx.Observable<Group>>() {
                @Override
                public rx.Observable<Group> call(List<Group> groups) {
                    return rx.Observable.from(groups);
                }
            }).flatMap(new Func1<Group, rx.Observable<Result<Group>>>() {
                @Override
                public rx.Observable<Result<Group>> call(Group group) {
                    return api.getApplicant(group.id);
                }
            }).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<Result<Group>>() {
                        List<Group> list =new ArrayList<Group>();
                        @Override
                        public void onCompleted() {
                            RecylerApplicantsAdapter adapter = new RecylerApplicantsAdapter(ApplicantActivity.this,list);
                            Log.v(TAG,new Gson().toJson(list));
                            rv.setAdapter(adapter);
                            rv.setLayoutManager(new LinearLayoutManager(ApplicantActivity.this));
                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(Result<Group> groupResult) {
                            if(!groupResult.data.users.isEmpty())
                                list.add(groupResult.data);
                        }
                    });
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void init() {

    }
}
